<!--
 * @Author: zhw 443844039@qq.com
 * @Date: 2025-08-02 19:19:45
 * @LastEditors: zhw 443844039@qq.com
 * @LastEditTime: 2025-08-08 01:33:11
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
  <div class="pageList">
    <TheTable
      ref="TheTableRef"
      :tableApi="tableApi"
      :searchList="searchList"
      :listColumns="listColumns"
      :actionArr="actionArr"
      @handleClick="handleClick"
    />
    <TheDetail ref="TheDetailRef"/>
    <div class="pageFooterBtn">
      <el-button @click="$router.back()">返 回</el-button>
    </div>
  </div>
</template>
<script>
import TheTable from '@/componentsView/TheTable.vue';
import TheDetail from '../system/document/TheDetail.vue';
import { documentFavoriteDelApi,documentFavoriteListApi  } from '@/api/favorite'
import { documentRecommendDelApi, documentRecommendListApi } from '@/api/recommend'
import { searchList, listColumns, actionArr } from './data'
import { useDict } from '@/utils/dict//useDict'
export default {
  name: 'TheworkbenchTable',
  components: { TheTable, TheDetail },
  data(){
    return {
      treeData: [],
      searchList,
      listColumns,
      actionArr,
      action: this.$route.query.action,
      tableApi: this.$route.query.action == "1" ? documentFavoriteListApi : documentRecommendListApi,
    }
  },
  created() {
    this.setDocumentFun()
  },
  methods:{
    handleClick({action, record}) {
      switch(action) {
        case 'look':
          this.$refs.TheDetailRef.handleOpen('look', { wordId: record.wordId, treeData: this.treeData });
        break;
        case 'del':
          this.$confirm('此操作将分享移除, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            const id = record.favoriteId?record.favoriteId:record.recommendId;
            const api = record.favoriteId ? documentFavoriteDelApi : documentRecommendDelApi;
            if (!id) return this.$message.error('id不存在');
            this.handleRemoveFun(id,api);
          }).catch(() => {
            this.$message.info('已取消移除');
          });
          break;
        default:
          break;
      }
    },
    // 删除文档操作
    handleRemoveFun(id,api){
      api(id).then(res=>{
        if (res.code === 200) {
          this.$message.success(res.msg)
          this.$refs.TheTableRef.resetQuery()
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    setDocumentFun(){
      let document_type = useDict('document_type')
      if (document_type.length > 0) {
        this.treeData = document_type.map(node => this.transformNode(node));
      }
    },

    transformNode(node) {
      // 创建新对象，只包含需要的属性
      const newNode = {
        label: node.dictLabel,
        id: node.dictCode
      };
      // 如果有子节点，递归处理子节点
      if (node.children && node.children.length) {
        newNode.children = node.children.map(child => this.transformNode(child));
      }
      return newNode;
    },
  }
}
</script>
